<template>
  <p-modal
    :title="title"
    :width="1200"
    :visible="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel">
    <p-spin :spinning="confirmLoading">
      <!-- 主表单区域 -->
      <p-form :form="form">
        <p-row>

          <p-col :span="12">
            <p-form-item label="供应商编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'code', validatorRules.code]" placeholder="请输入供应商编码"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="供应商名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'name', validatorRules.name]" placeholder="请输入供应商名称"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="供应商简称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'abbreviation', validatorRules.abbreviation]" placeholder="请输入供应商简称"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="供应商描述" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'comment', validatorRules.comment]" placeholder="请输入供应商描述"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="供应商ELS账号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'supplierEls', validatorRules.supplierEls]" placeholder="请输入供应商ELS账号"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="本方ELS账号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'localEls', validatorRules.localEls]" placeholder="请输入本方ELS账号"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="付款方式代码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'paymentCode', validatorRules.paymentCode]" placeholder="请输入付款方式代码"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="付款方式名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'paymentMethod', validatorRules.paymentMethod]" placeholder="请输入付款方式名称"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="供应商类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'supplierType', validatorRules.supplierType]" placeholder="请输入供应商类型"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="一级分类" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'classifyFirst', validatorRules.classifyFirst]" placeholder="请输入一级分类"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="二级分类" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'classifySecond', validatorRules.classifySecond]" placeholder="请输入二级分类"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="国家代码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'countryCode', validatorRules.countryCode]" placeholder="请输入国家代码"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="更新人标识" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'updatetimeBy', validatorRules.updatetimeBy]" placeholder="请输入更新人标识"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="更新时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择更新时间" v-decorator="[ 'updatetimeTime', validatorRules.updatetimeTime]" :trigger-change="true" style="width: 100%"/>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="时间标识" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择时间标识" v-decorator="[ 'timestamp', validatorRules.timestamp]" :trigger-change="true" style="width: 100%"/>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="公司标识" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'companyId', validatorRules.companyId]" placeholder="请输入公司标识"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="工厂标识" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'factoryId', validatorRules.factoryId]" placeholder="请输入工厂标识"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="租户标识" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'clientId', validatorRules.clientId]" placeholder="请输入租户标识"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="替换人员标识" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'substituteId', validatorRules.substituteId]" placeholder="请输入替换人员标识"></p-input>
            </p-form-item>
          </p-col>

        </p-row>
      </p-form>

      <!-- 子表单区域 -->
      <p-tabs v-model="activeKey" @change="handleChangeTabs">
        <p-tab-pane tab="供应商档案明细表" :key="refKeys[0]" :forceRender="true">
          <supplier-detail-form ref="supplierDetailForm" @validateError="validateError"></supplier-detail-form>
        </p-tab-pane>
      
      </p-tabs>

    </p-spin>
  </p-modal>
</template>

<script>

  import pick from 'lodash.pick'
  import { FormTypes,getRefPromise } from '@/utils/JEditableTableUtil'
  import { JEditableTableMixin } from '@/mixins/JEditableTableMixin'
  import { validateDuplicateValue } from '@/utils/util'
  import SupplierDetailForm from './SupplierDetailForm.vue'
  import JDate from '@/components/jeecg/JDate'  

  export default {
    name: 'SupplierFileModal',
    mixins: [JEditableTableMixin],
    components: {
    SupplierDetailForm,
      JDate,
    },
    data() {
      return {
        labelCol: {
          span: 6
        },
        wrapperCol: {
          span: 16
        },
        labelCol2: {
          span: 3
        },
        wrapperCol2: {
          span: 20
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
          code: {rules: [
          ]},
          name: {rules: [
            {required: true, message: '请输入供应商名称!'},
          ]},
          abbreviation: {rules: [
          ]},
          comment: {rules: [
          ]},
          supplierEls: {rules: [
          ]},
          localEls: {rules: [
          ]},
          paymentCode: {rules: [
          ]},
          paymentMethod: {rules: [
          ]},
          supplierType: {rules: [
          ]},
          classifyFirst: {rules: [
          ]},
          classifySecond: {rules: [
          ]},
          countryCode: {rules: [
          ]},
          updatetimeBy: {rules: [
          ]},
          updatetimeTime: {rules: [
          ]},
          timestamp: {rules: [
          ]},
          companyId: {rules: [
          ]},
          factoryId: {rules: [
          ]},
          clientId: {rules: [
          ]},
          substituteId: {rules: [
          ]},
        },
        refKeys: ['supplierDetail', ],
        tableKeys:[],
        activeKey: 'supplierDetail',
        // 供应商档案明细表
        supplierDetailTable: {
          loading: false,
          dataSource: [],
          columns: [
          ]
        },
        url: {
          add: '/basic-business/basicSupplierFile/supplierFile/add',
          edit: '/basic-business/basicSupplierFile/supplierFile/edit',
          supplierDetail: {
            list: '/basic-business/basicSupplierFile/supplierFile/querySupplierDetailByMainId'
          },
        }
      }
    },
    methods: {
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        let fieldval = pick(this.model,'code','name','abbreviation','comment','supplierEls','localEls','paymentCode','paymentMethod','supplierType','classifyFirst','classifySecond','countryCode','updatetimeBy','updatetimeTime','timestamp','companyId','factoryId','clientId','substituteId')
        this.$nextTick(() => {
          this.form.setFieldsValue(fieldval)
          this.$refs.supplierDetailForm.initFormData(this.url.supplierDetail.list,this.model.id)
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
        }
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)

        return {
          ...main, // 展开
          supplierDetailList: this.$refs.supplierDetailForm.getFormData(),
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },
     popupCallback(row){
       this.form.setFieldsValue(pick(row,'code','name','abbreviation','comment','supplierEls','localEls','paymentCode','paymentMethod','supplierType','classifyFirst','classifySecond','countryCode','updatetimeBy','updatetimeTime','timestamp','companyId','factoryId','clientId','substituteId'))
     },

    }
  }
</script>

<style scoped>
</style>